'''
Author: Zhuangyu
Date: 2022-10-21 15:30:48
LastEditTime: 2022-10-21 15:46:28
'''
from casadi import*
import numpy as np
from Parameters import *
from RichardsModel_1D import *
import random

p=Loam()
Nr,Nt,Nz,dr,dt,dz,Np,Na,Np,N_obs,Hz=circular_parameters()
DeltaT,Nsim=time_parameters()

Nu=1


x=SX.sym('x',Nz)
u=SX.sym('u',Nu)
ui=SX.sym('ui',Na)
DeltaT=SX.sym('DeltaT',1)

def autoNorm(data):

    mins = data.min(0)
    maxs = data.max(0)
    ranges = -(maxs-mins)
    normData = np.zeros(np.shape(data))
    row = data.shape[0]
    normData = data-np.tile(mins,(row,1))
    normData = normData/np.tile(ranges,(row,1))
    return normData



def pre_jacobian(x,u,ui,DeltaT):
    
    F = ODE_discrete(x,u,ui,DeltaT)
    H=getOutputs1D(x)
    h_jacx=jacobian(H,x)
    F_jacx=jacobian(F,x)
    F_jacu=jacobian(F,u)
    F_jacui=jacobian(F,ui)
    
    return F_jacx,F_jacu,F_jacui,h_jacx

F_symbol1=pre_jacobian(x,u,ui,DeltaT)[0]
F_symbol2=pre_jacobian(x,u,ui,DeltaT)[1]
F_symbol3=pre_jacobian(x,u,ui,DeltaT)[2]
H_symbol=pre_jacobian(x,u,ui,DeltaT)[3]


F1=Function('F1',[x,u,ui,DeltaT],[F_symbol1])
F2=Function('F2',[x,u,ui,DeltaT],[F_symbol2])
F3=Function('F3',[x,u,ui,DeltaT],[F_symbol3])
Fy=Function('Fy',[x,u,ui,DeltaT],[H_symbol])